<?php
class MakeExamController extends ExtendedController
{
    function __construct()
    {
        parent::__construct();

        if(Session::getInstance()->get('permission') != '1')
        {
            $_SESSION['ref'] = substr($_SERVER['PATH_INFO'],1);
            Utility::error('Bạn không đủ quyền hạn để sử dụng chức năng này', 'login');
            exit();
        }
    }
    
    public function index($classID=null, $message=null)
    {
        $makeExamModel = $this->loadModel('makeexam');
        if ($classID == null)
        {
            $id = (isset($_POST['id'])) ? $_POST['id'] : null;
            $subject = (isset($_POST['subject'])) ? $_POST['subject'] : null;
            $lecture = (isset($_POST['lecture'])) ? $_POST['lecture'] : null;

            $classes=null;
            $classes = $makeExamModel->getClassList($id, $subject, $lecture);

            $classesView =  $this->loadView('classes');
            $classesView->appendBlockData(array('classes'=>$classes));
            $classesView->appendBlockData(array('message'=>$message));
            $classesView->setBlockData("Chọn lớp học","main_header");

            $classesView->render();
        }
        else
        {
            // retrive data
            $classes = $makeExamModel->getClassList($classID, null, null);

            // assign data to view
            $makeExamView = $this->loadView('makeexam');
            $makeExamView->appendBlockData(array('class'=>$classes[0]));
            $makeExamView->appendBlockData(array('errorMessage'=>$message));
            $makeExamView->setBlockData("Tạo đề thi","main_header");

            // render
            $makeExamView->render();
        }
    }
    
    public function save()
    {
        $classid = $_POST['classid'];
        $subjectid = $_POST['subjectid'];
        $day = $_POST['day'];
        $month = $_POST['month'];
        $year = $_POST['year'];
        $hour = $_POST['hour'];
        $minute = $_POST['minute'];
        $isReal = $_POST['real'];
        
        $makeExamModel = $this->loadModel('makeexam');
        $result = $makeExamModel->save($classid, $subjectid, $day, $month, $year, $hour, $minute, $isReal);
        if ($result['check'])
            Utility::redirect("makeexam");
        else $this->index($classid, $result['message']);
    }

    public function export($subjectID, $subject)
    {
        global $_app_config;
        if(Session::getInstance()->get('permission') != '1')
        {
            $_SESSION['ref'] = substr($_SERVER['PATH_INFO'],1);
            Utility::error('Bạn không đủ quyền hạn để sử dụng chức năng này', 'login');
            exit();
        }
        $this->loadLibrary('DocGenerator');
        $makeExamModel = $this->loadModel('makeexam');
        $quesions = $makeExamModel->getQuestions($subjectID);
        if (count($quesions) == 0)
        {
            $this->index();
            return;
        }
        $generator = new DocGenerator();
        $exportView = $this->loadView('export');
        $exportView->appendBlockData(array('subject'=>$subject));
        $exportView->appendBlockData(array('subjectid'=>$subjectID));
        $exportView->appendBlockData(array('questions'=>$quesions));
        $resource = $exportView->renderBlock($_app_config['masterTemplate']['default_render_block']);
        $generator->addParagraph($resource);
        $generator->setDocumentCharset('utf-8');
        $generator->output($subjectID . '_' . $subject . '.doc');
    }
}
?>